package com.lin.servlet;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.lin.service.AdminServiceImpl;
import com.lin.service.IAdminSevice;
import com.lin.utils.BaseServlet;
import com.lin.vo.Admin;

@WebServlet(name = "AdminServlet", urlPatterns = {"/adminServlet"})
public class AdminServlet extends BaseServlet {

    private static final long serialVersionUID = 1L;
    private IAdminSevice adminService = new AdminServiceImpl();

    public String login(HttpServletRequest request, HttpServletResponse response) {
        /**
         * 1.封装表单数据
         */
        Admin admin = new Admin();
        try {
            BeanUtils.populate(admin, request.getParameterMap());
            /**
             * 2.调用业务层方法
             */
            boolean flag = adminService.login(admin);
            if (!flag) {
                request.setAttribute("msg", "用户或者密码错误");
                return "change:/adminjsps/login.jsp";
            } else {
                request.getSession().setAttribute("admin", admin);
                return "orient:/adminjsps/admin/main.jsp";
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

}
